package ru.spb.osll.myfit.fixtures;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import ru.spb.osll.myfit.fixtures.base.OneLineFixture;
import ru.spb.osll.myfit.fixtures.log.MyFitLogger;

public class WaitForFixture extends OneLineFixture {
  
  public static final Pattern INTERVAL_PATTERN = Pattern.compile("([0-9]+)\\s+(minute|second)");

  public boolean waitFor(String interval) {
    Logger Log = MyFitLogger.getLogger(WaitForFixture.class); 
    Log.info("wait " + interval);
    
    Matcher matcher = INTERVAL_PATTERN.matcher(interval);
    if (matcher.matches()) {
      String number = matcher.group(1);
      String measure = matcher.group(2);
      Long timeLong = Long.valueOf(number);

      if (measure.equals("minute")) {
        timeLong *= 60000;
      } else if (measure.equals("second")) {
        timeLong *= 1000;
      }

      try {
        Thread.sleep(timeLong);
      } catch (InterruptedException e) {

      }
      return true;
    }
    return true;
  }

  public static String[] getTestTableTemplates() {
    return new String[] {"|wait for|1 minute|"};
  }
}
